﻿#region Library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConnectDatabase;
using System.Configuration;
using GSM.BusinessLayer;
#endregion

namespace CRM_SMS_MARKETING
{
    class BLL_SendMessage
    {
        #region Attributes
        ConnectDB con = new ConnectDB(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        Customer_MessageFactory customer_MessageFactory = new Customer_MessageFactory();
        SmsControl.SmsControl modem;
        List<Customer_Message> OutboxList;
        #endregion

        #region Method
        public BLL_SendMessage(SmsControl.SmsControl Modem)
        {
            modem = Modem;
        }
        public void send()
        {
            try
            {
                OutboxList = customer_MessageFactory.GetAllBy(Customer_Message.Customer_MessageFields.Type, 2);
                modem.Send(OutboxList);
                UpdateSentList(OutboxList);

                //Check if there is any message need to be sent
                OutboxList = OutboxList = customer_MessageFactory.GetAllBy(Customer_Message.Customer_MessageFields.Type, 2);
                if (OutboxList.Count != 0)
                    send();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        void UpdateSentList(List<Customer_Message> list)
        {
            try
            {
                foreach (Customer_Message i in list)
                {
                    customer_MessageFactory.Update(i);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
